package com.medical.view.admin.user;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormValidatingBehavior;
import org.apache.wicket.markup.html.form.Form;

import com.medical.model.admin.Account;
import com.medical.model.admin.AdminDbOperation;
import com.medical.model.admin.ListLookUp;
import com.medical.model.admin.Personal;
import com.medical.model.admin.Role;
import com.medical.view.wicket.Model.MedCompoundModel;
import com.medical.view.wicket.Model.MedPropertyModel;
import com.medical.view.wicket.ajax.html.MedAjaxButton;
import com.medical.view.wicket.ajax.html.MedAjaxEmailField;
import com.medical.view.wicket.ajax.html.MedAjaxForm;
import com.medical.view.wicket.ajax.html.MedAjaxSaveButton;
import com.medical.view.wicket.ajax.html.MedAjaxTelephoneField;
import com.medical.view.wicket.ajax.html.MedAjaxTextArea;
import com.medical.view.wicket.ajax.html.MedAjaxTextField;
import com.medical.view.wicket.ajax.html.MedAjaxYearDateField;
import com.medical.view.wicket.form.MedCheckBox;
import com.medical.view.wicket.form.MedChoiceRenderer;
import com.medical.view.wicket.form.MedDropDownChoice;
import com.medical.view.wicket.form.MedPasswordTextField;
import com.medical.view.wicket.html.modal.MedPop;
import com.medical.view.wicket.html.panal.MedFeedbackPanel;
import com.own.core.html.panel.OwnPanel;

public class AddUserPanel extends OwnPanel {

	public AddUserPanel(String s) {
		 
		add(new AddUserForm("add_form"));

	}
}

class AddUserForm extends MedAjaxForm {

	Personal user = new Personal(new Account());

	public AddUserForm(String id) {
		super(id);
		setModel(new MedCompoundModel(user));

		// feedback panel
		MedFeedbackPanel f = new MedFeedbackPanel("feedback");
		add(f);

		// account text
		MedAjaxTextField accountTxt = new MedAjaxTextField(
				"account.accountName");
		accountTxt.setRequired(true);
		add(accountTxt);

		// password text
		MedPasswordTextField passTxt = new MedPasswordTextField(
				"account.password");
		passTxt.setRequired(true);
		add(passTxt);

		// Role dropDown
		List<Role> roles = AdminDbOperation.listAllRole();
		MedChoiceRenderer<Role> choiceRenderer = new MedChoiceRenderer<Role>(
				"roleName", "id");

		MedDropDownChoice roleDropDown = new MedDropDownChoice("Role",
				new MedPropertyModel(user, "account.Role"), roles, choiceRenderer);
		roleDropDown.setRequired(true);
		add(roleDropDown);

		// name text
		MedAjaxTextField nameTxt = new MedAjaxTextField("name");
		nameTxt.setRequired(true);
		add(nameTxt);

		// tele text
		MedAjaxTelephoneField teleTxt = new MedAjaxTelephoneField("tele");
		add(teleTxt);

		// mobile text
		MedAjaxTelephoneField mobilTxt = new MedAjaxTelephoneField("mobil");
		add(mobilTxt);

		// email text
		MedAjaxEmailField emailTxt = new MedAjaxEmailField("email");
		add(emailTxt);

		// address text
		MedAjaxTextArea addressTxt = new MedAjaxTextArea("address");
		add(addressTxt);

		// birthdate text
		MedAjaxYearDateField birthDatelTxt = new MedAjaxYearDateField(
				"brithDate");
		add(birthDatelTxt);

		// gender radio
		MedCheckBox genderCheck = new MedCheckBox("gender");
		add(genderCheck);

		// marity status

		List<ListLookUp> marityStatus = Arrays.asList(new ListLookUp(1l, "1"),
				new ListLookUp(2l, "2"), new ListLookUp(3l, "3"),
				new ListLookUp(4l, "4"));
		MedChoiceRenderer<ListLookUp> marityChoiceRenderer = new MedChoiceRenderer<ListLookUp>(
				"name", "id");

		MedDropDownChoice marityStatusDropDown = new MedDropDownChoice(
				"marity", new MedPropertyModel(user, "marity"), marityStatus,
				marityChoiceRenderer);
		add(marityStatusDropDown);

		// smoker
		MedCheckBox smokercheck = new MedCheckBox("smoker");
		add(smokercheck);

		// insurance
		List<ListLookUp> insurance = Arrays.asList(new ListLookUp(1l, "1"),
				new ListLookUp(2l, "2"), new ListLookUp(3l, "3"),
				new ListLookUp(4l, "4"));
		MedDropDownChoice insuranceDropDown = new MedDropDownChoice(
				"insurance", new MedPropertyModel(user, "insurance"), insurance,
				marityChoiceRenderer);
		add(insuranceDropDown);

		// note note
		MedAjaxTextArea noteTxt = new MedAjaxTextArea("note");
		add(noteTxt);

		MedAjaxSaveButton addBut = new MedAjaxSaveButton("Add") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// super.onSubmit();
//				user.getAccount().setId(null);
				AdminDbOperation.saveOrUpdate(user.getAccount() );
				AdminDbOperation.saveOrUpdate(user );
//			((MedApp	)form.getApplication()).getEntityManagerFactory();
////				System.out.println(user);
//				// setVisibilityAllowed(false);
//				((MedPop) this.getParent().getParent().getParent())
//						.closeCurrent(target);
//				
//				form.getParent().detach();
//				

			}

		};

		add(addBut);

		MedAjaxSaveButton addContinueButton = new MedAjaxSaveButton(
				"addAndContinue") {

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				super.onSubmit();

//				AdminDbOperation.saveOrUpdate(user);

				System.out.println(user);
				user = new Personal(new Account());
				form.clearInput();
				form.setDefaultModel(new MedCompoundModel(user));
				// form.getModel().`
				// form.render();
				
				form.clearInput();
//				form.modelChanged();
//				form.size();
				target.addComponent(AddUserForm.this);
				// for(int i=0;i<form.size();i++){
				//					
				// form.get(i).
				// }
				// form.renderComponent();
				// form.rendered();
				// target.
			}

		};
		// addContinueButton.updateModel();

		add(addContinueButton);
		MedAjaxButton closeBut = new MedAjaxButton("close") {

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				((MedPop) this.getParent().getParent().getParent())
						.closeCurrent(target);
				// ((Panel)
				// this.getParent().getParent().getParent().getParent())
				// target.getPage().renderComponent();
				
			}

		};
		closeBut.setDefaultFormProcessing(false);

		add(closeBut);

		AjaxFormValidatingBehavior.addToAllFormComponents(this, "onblur");
	}

}
 